jQuery(document).ready(function($) {
	$('#add_question').click(function(){
		// if($("#is_save").val() == "true"){
			var question = "<div class='izboxQuestion'>";
			question = question+"<div class='save_question_db'></div>";
			// question = question+"<label for='izQuestionTitle'>Question<span class='remove_question'>- Remove Question</span><span class='save_question'>+ Save Question</span></label><input type='text' name='izQuestion' id='izQuestion' class='izQuestion'/>";
			question = question+"<label for='izQuestionTitle'>Question<span class='remove_question'>+ Remove Question</span></label><input type='text' name='izQuestion' id='izQuestion' class='izQuestion'/>";
			question = question+"<label for='izAnswerTitle'>Answer<div class='save_answer_db'></div><span class='add_answer'>+ Add Answer</span></label>";
			question = question+"<input type='hidden' name='question_id' class='question_id' />";
			question = question+"<div class='izboxAnswer'></div>";
			question = question+"</div>";
			$('#izMainQuestion').append($(question).fadeIn(500));
		// }else{
			// alert("Please complete practice test information");
			// return false;
		// }
		return false;
	});

	$(document).on('click','.updateInput',function(){
		// var input = $(this).parent().find('.add');
		// var input_val = input.val();
		// input.attr('type','text');
		// input.val(input_val);
		// input.next().remove();
		// $(this).removeClass('updateInput').fadeOut().addClass('setInput').text("+ Apply").fadeIn();
		// Update code : 5/2/2014 - Editable
		var input = $(this).parent().find('.add');
		var input_val = input.val();
		input.attr('type','text');
		input.val(input_val);
		input.focus();
		$(this).hide();
	});

	$(document).on('click','.izQuestion',function(){
		// Update code : 5/2/2014 - Editable
		if($(this).is('div')){
			var parent_el = $(this).parent();
			var question = $(this).text();
			$(this).replaceWith($("<input type='text' name='izQuestion' value='"+question+"' id='izQuestion' class='izQuestion'/>"));
			parent_el.find('input.izQuestion').focus();
		}
	});

	$(document).on('click','#ask_use',function(){
		$('#asknote').slideToggle(500);
	});

	$(document).on('click','.add_answer',function(){
		if($(this).parent().parent().find('.question_id').val() == "" /*|| $(this).parent().prev().text().length <=6*/){
			// alert("Please insert question before create the answers");
			// return false;
		}else{
			// Update code : 9/2/2014 - Add full answer
			var answer = '';
			var el_answer = $(this).parent().next().next().find('.addAnswer');
			if(el_answer.length ==0 && confirm("Is Yes-No Question ?")) {
				answer = "<div class='answer'><label for='true'><input type='radio' name='izAddAnswer_truefalse' class='izAddAnswer' value='True'/>True</label><label for='false'><input type='radio' name='izAddAnswer_truefalse' class='izAddAnswer' value='False'/>False</label><input type='hidden' name='answer_id' class='answer_id' /></div>";
				$(this).attr('data-question','true');
				$(this).removeClass('add_answer').fadeOut().addClass('save_answer').text("+ Save Answer").fadeIn();
			}else if(el_answer.length >= 1 && el_answer.length <4){
				var stt = changeLabel(el_answer.length+1);
				answer = "<div class='addAnswer'><label>"+stt+"<input type='text' class='add izAddAnswer' name='izAddAnswer'/><input type='hidden' name='answer_id' class='answer_id' /></label><span class='updateInput'></span></div>";
				if(el_answer.length == 3){
					$(this).removeClass('add_answer').fadeOut().addClass('save_answer').text("+ Save Answer").fadeIn();
				}
			}else{
				answer = "<div class='addAnswer'><label>A.<input type='text' class='add izAddAnswer' name='izAddAnswer'/><input type='hidden' name='answer_id' class='answer_id' /></label><span class='updateInput'></span></div>";
				answer = answer+"<div class='addAnswer'><label>B.<input type='text' class='add izAddAnswer' name='izAddAnswer'/><input type='hidden' name='answer_id' class='answer_id' /></label><span class='updateInput'></span></div>";
				answer = answer+"<div class='addAnswer'><label>C.<input type='text' class='add izAddAnswer' name='izAddAnswer'/><input type='hidden' name='answer_id' class='answer_id' /></label><span class='updateInput'></span></div>";
				answer = answer+"<div class='addAnswer'><label>D.<input type='text' class='add izAddAnswer' name='izAddAnswer'/><input type='hidden' name='answer_id' class='answer_id' /></label><span class='updateInput'></span></div>";
				$(this).removeClass('add_answer').fadeOut().addClass('save_answer').text("+ Save Answer").fadeIn();
			}
			$(this).parent().next().next().append($(answer).fadeIn(500));
		}
		return false;
	});

	$('#show_information').click(function(){
		if($("#is_save").val() == "true"){
			$('#izPracticetestInfo').slideToggle('normal');
		}else{
			alert("Please complete practice test information before hide this box");
			return false;
		}
		return false;
	});
});

function changeLabel(number)
{
	switch(number){
		case 1:
			return "A.";
			break;
		case 2:
			return "B.";
			break;
		case 3:
			return "C.";
			break;
		case 4:
			return "D.";
			break;
		default:
			return "A.";
			break;
	}
}